package com.todo.strategy.online.application.frontend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.enums.EnableTypeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.strategy.online.application.convert.StrategyConvert;
import com.todo.strategy.online.application.entity.bo.MemoTemplateBO;
import com.todo.strategy.online.application.entity.bo.MemoUserRelationBO;
import com.todo.strategy.online.application.entity.bo.PlanSetBO;
import com.todo.strategy.online.application.frontend.service.MemoUserRelationAggService;
import com.todo.strategy.online.application.service.MemoTemplateService;
import com.todo.strategy.online.application.service.MemoUserRelationService;
import com.todo.strategy.online.application.service.PlanSetService;
import com.todo.strategy.online.iface.request.*;
import com.todo.strategy.online.iface.response.MemoUserRelationDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author azhebuxing
 * @date 2024/11/29 19:10
 * @description
 */
@Slf4j
@Service("memoUserRelationAggService")
public class MemoUserRelationAggServiceImpl implements MemoUserRelationAggService {

    @Resource
    private MemoUserRelationService memoUserRelationService;
    @Resource
    private MemoTemplateService memoTemplateService;
    @Resource
    private PlanSetService planSetService;


    @Override
    public BizResponse<Boolean> buildMemoUserRelationByTemplate(BuildMemoUserRelationByTemplateParam param) {
        Long memoTemplateId = param.getMemoTemplateId();
        MemoTemplateBO memoTemplateBO = memoTemplateService.queryMemoTemplateById(memoTemplateId);
        if (memoTemplateBO == null) {
            log.error("MemoUserRelationAggService#buildMemoUserRelationByTemplate 拷贝构建备忘录关联失败，模板数据不存在，param:{}", JSONObject.toJSONString(param));
            throw new BizException(BizErrorCodeEnum.OPERATION_FAILED, "当前数据异常，请刷新后操作");
        }
        if (EnableTypeEnum.FORBIDDEN.equals(memoTemplateBO.getStatus())) {
            log.error("MemoUserRelationAggService#buildMemoUserRelationByTemplate 拷贝构建备忘录关联失败，模板数据已禁用，param:{}", JSONObject.toJSONString(param));
            throw new BizException(BizErrorCodeEnum.OPERATION_FAILED, "当前数据调整中，请稍后重试");
        }
        PlanSetBO planSetBO = planSetService.queryPlanSetById(param.getPlanId());
        if (planSetBO == null) {
            log.error("MemoUserRelationAggService#buildMemoUserRelationByTemplate 拷贝构建备忘录关联失败，关联计划数据不存在，param:{}", JSONObject.toJSONString(param));
            throw new BizException(BizErrorCodeEnum.OPERATION_FAILED, "当前数据异常，请刷新后操作");
        }
        if (!planSetBO.getUserId().equals(param.get_user().getUserId())) {
            log.error("MemoUserRelationAggService#buildMemoUserRelationByTemplate 拷贝构建备忘录关联失败，关联计划数据不属于操作人，param:{}", JSONObject.toJSONString(param));
            throw new BizException(BizErrorCodeEnum.OPERATION_FAILED, "当前计划清单不属于本人，请刷新后操作");
        }

        MemoUserRelationBO request = StrategyConvert.buildInsertMemoUserRelationByTemplate(memoTemplateBO, param);
        Boolean inserted = memoUserRelationService.insertMemoUserRelation(request);
        log.info("MemoUserRelationAggService#buildMemoUserRelationByTemplate 拷贝构建备忘录关联完成 新增人Id：{}，结果:{}", param.get_user().getUserId(), inserted);
        return BizResponse.success(inserted);
    }

    @Override
    public BizResponse<Boolean> updateMemoUserRelationById(UpdateMemoUserRelationByIdParam param) {
        Long memoUserRelationId = param.getMemoUserRelationId();
        MemoUserRelationBO memoUserRelationBO = memoUserRelationService.queryMemoUserRelationById(memoUserRelationId);
        if (memoUserRelationBO == null) {
            log.error("MemoUserRelationAggService#updateMemoUserRelationById 更新备忘录关联失败，关联数据不存在，关联Id：{}", memoUserRelationId);
            throw new BizException(BizErrorCodeEnum.OPERATION_FAILED, "当前数据异常，请刷新后操作");
        }
        if (!memoUserRelationBO.getUserId().equals(param.get_user().getUserId())) {
            log.error("MemoUserRelationAggService#updateMemoUserRelationById 更新备忘录关联失败，关联数据不属于操作人，关联Id：{}", memoUserRelationId);
            throw new BizException(BizErrorCodeEnum.OPERATION_FAILED, "当前数据不属于本人，请刷新后操作");
        }
        MemoUserRelationBO request = StrategyConvert.convertToMemoUserRelationBO(param);
        Boolean updated = memoUserRelationService.updateMemoUserRelation(request);
        log.info("MemoUserRelationAggService#updateMemoUserRelationById 更新备忘录关联完成，关联Id：{}，结果:{}", param.getMemoUserRelationId(), updated);
        return BizResponse.success(updated);
    }

    @Override
    public BizResponse<Boolean> removeMemoUserRelationById(RemoveMemoUserRelationByIdParam param) {
        MemoUserRelationBO memoUserRelationBO = memoUserRelationService.queryMemoUserRelationById(param.getMemoUserRelationId());
        if (memoUserRelationBO == null) {
            log.warn("MemoUserRelationAggService#removeMemoUserRelationById 删除备忘录关联完成，关联数据不存在，关联Id：{}", param.getMemoUserRelationId());
            return BizResponse.success(true);
        }
        if (!memoUserRelationBO.getUserId().equals(param.get_user().getUserId())) {
            log.error("MemoUserRelationAggService#removeMemoUserRelationById 删除备忘录关联完成，关联数据不属于当前用户，param：{},userId:{}", param, param.get_user().getUserId());
            throw new BizException(BizErrorCodeEnum.ILLEGAL_OPERATION, "当前数据不属于您，无法操作");
        }
        Boolean removed = memoUserRelationService.removeMemoUserRelationById(param.getMemoUserRelationId());
        log.info("MemoUserRelationAggService#removeMemoUserRelationById 删除备忘录关联完成，关联Id：{}，结果:{}", param.getMemoUserRelationId(), removed);
        return BizResponse.success(removed);
    }

    @Override
    public BizResponse<MemoUserRelationDetailVO> queryMemoUserRelationListByPlanId(QueryMemoUserRelationListByPlanIdParam param) {
        MemoUserRelationBO memoUserRelationBO = memoUserRelationService.queryMemoUserRelationByUserPlanId(param.get_user().getUserId(), param.getPlanId());
        MemoUserRelationDetailVO detailVO = StrategyConvert.convertToMemoUserRelationDetailVO(memoUserRelationBO);
        return BizResponse.success(detailVO);
    }

    @Override
    public BizResponse<MemoUserRelationDetailVO> queryMemoUserRelationById(QueryMemoUserRelationByIdParam param) {
        MemoUserRelationBO memoUserRelationBO = memoUserRelationService.queryMemoUserRelationById(param.getMemoUserRelationId());
        MemoUserRelationDetailVO detailVO = StrategyConvert.convertToMemoUserRelationDetailVO(memoUserRelationBO);
        return BizResponse.success(detailVO);
    }
}
